import json
import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

package_index = 0


def generatorHeader(mode, to: [list, dict]):
    global package_index
    package_index += 1
    return json.dumps({
        "mode": mode,
        "to": to,
        "index": package_index,
        "time": time.time()
    })


def createMessage(header: str, body: dict):
    return f"{header}||{json.dumps(body)}".encode("utf-8")


while True:
    s.sendto(createMessage(generatorHeader("register", [{"channelId": 0}]), {"position": {"x": 11, "y": 222}}),
             ('127.0.0.1', 9999))
    c = s.recvfrom(1024)
    print(c)

s.close()
